﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;

namespace AndreasJohansson.AutoMailic
{
  class DBConnection
  {
    public DBConnection(string type, string connectionString)
    {
      _type = type;
      _connectionString = connectionString;

      _dbConnection = GetDBConnection(type, connectionString);
    }

    public void Disconnect()
    {
      if (null != _dbConnection)
      {
        _dbConnection.Close();
        _dbConnection = null;
      }
    }

    public IDataReader GetItems(string sql)
    {
      _dbConnection.Open();
      IDbCommand dbCmd = _dbConnection.CreateCommand();
      dbCmd.CommandText = sql;
      dbCmd.CommandType = CommandType.Text;
      IDataReader reader = dbCmd.ExecuteReader();
      return reader;
    }

    private IDbConnection GetDBConnection(string type, string connectionString)
    {
      switch (type)
      {
        case "MSSQL":
          {
            return new SqlConnection(connectionString);
          }
        case "ODBC":
          {
            return new OdbcConnection(connectionString);
          }
        default:
          {
            return null;
          }
      }
    }



    private string _type;
    private string _connectionString;
    private IDbConnection _dbConnection;
  }


}
